package cr.co.tse.padronelectoral.parser;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import au.com.bytecode.opencsv.CSVReader;
import cr.co.tse.padronelectoral.csv.OpenCsvRowReaderGenericDAOImpl;
import cr.co.tse.padronelectoral.csv.RowReaderGenericDAO;

/**
 * This is class is a helper to parser csv local files.
 * @author jsanca
 *
 */
public class ParserCsvFileHelper {

	/**
	 * Call this method to parser the csv file.
	 * You must pass the rowProcessorHandler, to process each row.
	 * @param file File
	 * @param rowProcessor RowProcessorHandler
	 * @throws IOException
	 */
	public void parser(File file, RowProcessorHandler rowProcessor) throws IOException {

		CSVReader csvReader = null;
		RowReaderGenericDAO csvReaderGenericDAO = null;
		String [] row = null;
		
		try {
			
			csvReader = new CSVReader(new FileReader(file));
			csvReaderGenericDAO = new OpenCsvRowReaderGenericDAOImpl(csvReader);
			
			while (null != (row = csvReaderGenericDAO.next())) {
			
				rowProcessor.process (row);
			}
		}  finally {
			
			if (null != csvReader) {
				
				try {
					csvReader.close();
				} catch (IOException e) {
					/** Quiet */
				}
			}
		}
		
	} // parser.
} // E:O:F:ParserCsvFileHelper.
